<?php
namespace app\admin\controller;
use think\Db;
use app\admin\model\ProductModel;
use app\admin\model\StudentModel;
use app\admin\model\FinanceModel;
use app\admin\model\MemberModel;
use app\admin\model\OrderModel;
use app\admin\controller\Product;
use app\admin\controller\Education;
use app\admin\controller\Finance;
class Trash extends Base
{
	#产品
	/**
	 * [回收站产品列表]
	 */
	public function product(){
		if(request()->isAjax()){
			$ProModel = new ProductModel();
			$ProCtrler = new Product();
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $whereOr = $ProCtrler->seaWhereOr($param);
            $return['rows'] = $ProModel->getInfoByWhere(['delete_status'=>1],$whereOr,true,$offset,$limit,'id desc');
            $whereOr = $ProCtrler->seaWhereOr($param);
            $return['total'] = $ProModel->getInfoByWhere(['delete_status'=>1],$whereOr,false);
            foreach($return['rows'] as $key =>$vo){
                $return['rows'][$key]['operate'] = showOperate($this->mkProListBtn($vo['id']));
            }
            return json($return);
        }
        return $this->fetch();
	}

	/**
	 * [产品恢复]
	 */
	public function recoveryPro(){
		$ProModel = new ProductModel();
		$param = input('post.');
		$ids = explode(",",$param['ids']);
		return $ProModel->productRecovery($ids);
	}

	/**
	 * [产品删除]
	 */
	public function delPro(){
		$ProModel = new ProductModel();
		$param = input('post.');
		$ids = explode(",",$param['ids']);
		return $ProModel->productDelete($ids);
	}

	private function mkProListBtn($id){
		return [
			'<i class="fa fa-retweet"></i>' => [
                'auth' => 'trash/recoverypro',
                'href' => "javascript:recoveryPro(" . $id . ")",
                'btnStyle' => 'primary',
                'icon' => ''
            ],
            '<i class="fa fa-trash-o">' => [
                'auth' => 'trash/delpro',
                'href' => "javascript:delPro(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => ''
            ],

        ];
	}

	#学员
	public function student(){
		if(request()->isAjax()){
			$STModel = new StudentModel();
			$STCtrler = new Education();
            $param = input('param.');
			$param['seaName'] ? $whereName=['name'=>['like','%'.$param['seaName'].'%']] : $whereName = [];
			$param['seaPhone'] ? $wherePhone=['phone'=>['like','%'.$param['seaPhone'].'%']] : $wherePhone = [];
			$param['seaSchool'] ? $whereSchool=['school'=>['like','%'.$param['seaSchool'].'%']] : $whereSchool = [];
			$param['seaMajor'] ? $whereMajor=['major'=>['like','%'.$param['seaMajor'].'%']] : $whereMajor = [];
			$param['seaBatch'] ? $whereBatch=['batch'=>['like','%'.$param['seaBatch'].'%']] : $whereBatch = [];
			$param['seaClass'] ? $whereClass=['class'=>['like','%'.$param['seaClass'].'%']] : $whereClass = [];
			($param['seaSchoolRoll']!='') ? $whereSchoolRoll=['school_roll'=>$param['seaSchoolRoll']] : $whereSchoolRoll = [];
			($param['seaRollStatus']!='') ? $whereRollStatus=['roll_status'=>$param['seaRollStatus']] : $whereRollStatus = [];
			$where = ['delete_status'=>1];
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $return['rows'] = $STModel->getInfoByWhere(
				$where,$whereName,$wherePhone,$whereSchool,$whereMajor,$whereBatch,$whereClass,$whereSchoolRoll,$whereRollStatus,
				true,$offset,$limit
			);
            $return['total'] = $STModel->getInfoByWhere(
				$where,$whereName,$wherePhone,$whereSchool,$whereMajor,$whereBatch,$whereClass,$whereSchoolRoll,$whereRollStatus
			);
			$return['rows'] = $STCtrler->translateStudent($return['rows']);
			foreach($return['rows'] as $key =>$vo){
                $return['rows'][$key]['operate'] = showOperate($this->mkSTListBtn($vo['id']));
            }
            return $return;
        }
        $schools = DB::name('school_specialty')->where('pid',0)->select();
        $this->assign([
            'schools' => $schools
        ]);
        return $this->fetch();
	}

	/**
	 * [学员恢复]
	 */
	public function STRecovery(){
		$STModel = new StudentModel();
		$param = input('post.');
		$ids = explode(",",$param['ids']);
		return $STModel->STRecovery($ids);
	}

	/**
	 * [学员删除]
	 */
	public function STdelete(){
		$STModel = new StudentModel();
		$param = input('post.');
		$ids = explode(",",$param['ids']);
		return $STModel->STDelete($ids);
	}

	private function mkSTListBtn($id){
		return [
			'<i class="fa fa-retweet"></i>' => [
                'auth' => 'trash/strecovery',
                'href' => "javascript:recoveryST(" . $id . ")",
                'btnStyle' => 'primary',
                'icon' => ''
            ],
            '<i class="fa fa-trash-o">' => [
                'auth' => 'trash/stdelete',
                'href' => "javascript:deleteST(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => ''
            ],

        ];
	}

	#流水
	public function account(){
		$FincModel = new FinanceModel();
		$FinCtrler = new Finance();
		if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
			$where = ['delete_status' => 1];
			$sortName = input('sortName');
			$sortOrder = input('sortOrder');
			$orderBy = $sortName." ".$sortOrder;
			$FinCtrler->WhereSn($param['seaSn'],$FincModel);
			$FinCtrler->WhereName($param['seaName'],$FincModel);
			$FinCtrler->WhereSource($param['seaSource'],$FincModel);
			$FinCtrler->WhereStatus($param['seaStatus'],$FincModel);
			$FinCtrler->WhereTime($param['seaTime'],$FincModel);
            $return['rows'] = $FincModel->getInfoByWhere($where,true,$offset,$limit,$orderBy);
			$FinCtrler->WhereSn($param['seaSn'],$FincModel);
			$FinCtrler->WhereName($param['seaName'],$FincModel);
			$FinCtrler->WhereSource($param['seaSource'],$FincModel);
			$FinCtrler->WhereStatus($param['seaStatus'],$FincModel);
			$FinCtrler->WhereTime($param['seaTime'],$FincModel);
            $return['total'] = $FincModel->getInfoByWhere($where);
			$return['rows'] = $FinCtrler->translateFince($return['rows']);
			foreach($return['rows'] as $key =>$vo){
                $return['rows'][$key]['operate'] = showOperate($this->mkAccListBtn($vo['id']));
            }
            return $return;
        }
        return $this->fetch();
	}

	/**
	 * [流水恢复]
	 */
	public function AccRecovery(){
		$FincModel = new FinanceModel();
		$param = input('post.');
		$ids = explode(",",$param['ids']);
		return $FincModel->AccRecovery($ids);
	}

	/**
	 * [流水删除]
	 */
	public function AccDelete(){
		$FincModel = new FinanceModel();
		$param = input('post.');
		$ids = explode(",",$param['ids']);
		return $FincModel->AccDelete($ids);
	}

	private function mkAccListBtn($id){
		return [
			'<i class="fa fa-retweet"></i>' => [
                'auth' => 'trash/accrecovery',
                'href' => "javascript:recoveryAcc(" . $id . ")",
                'btnStyle' => 'primary',
                'icon' => ''
            ],
            '<i class="fa fa-trash-o">' => [
                'auth' => 'trash/accdelete',
                'href' => "javascript:deleteAcc(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => ''
            ],

        ];
	}

	#-------------------------------------------< 分割线 >-----------------------------------------------------------#
	/**
	 * [客户/会员回收站] 删除列表 恢复 删除
	 * 黄
	 */
	public function member ()
	{
		if(request()->isAjax()){
			$MemModel = new MemberModel();
			$param = input('param.');
			$limit = $param['pageSize'];
			$offset = ($param['pageNumber'] - 1) * $limit;
			$where = [];
			if (!empty($param['name'])) {
				$where['name'] = ['like', '%' . $param['name'] . '%'];
			}
			if (!empty($param['phone'])) {
				$where['phone'] = ['like', '%' . $param['phone'] . '%'];
			}
			$where['delete_status'] = ['neq',1];

			$memTrArr = $MemModel->getMemberByWhere($where,$offset,$limit);

			foreach($memTrArr as $key=>$vo){
				switch ($vo['status']) {
					case 1:
						$memTrArr[$key]['status'] = '<span style="color: #1ab394;">跟踪</span>';
						$memTrArr[$key]['operate'] = showOperate($this->mkMemberBtn($vo['id']));
						break;
					case 2:
						$memTrArr[$key]['status'] = '<span style="color: red;">成交</span>';
						$memTrArr[$key]['operate'] = showOperate($this->mkMemberBtn($vo['id']));
						break;
					case 3:
						$memTrArr[$key]['status'] = '释放';
						$memTrArr[$key]['operate'] = showOperate($this->mkMemberBtn($vo['id']));
						break;
					default:
						$memTrArr[$key]['status'] = '资料库客源';
						$memTrArr[$key]['operate'] = showOperate($this->mkMemberBtn($vo['id']));
				}
			}
			$return['total'] = $MemModel->getAllMember($where);  // 总数据
			$return['rows'] = $memTrArr;
			return json($return);
		}
		return $this->fetch();
	}
	/**
	 * [客户/会员恢复]
	 */
	public function recoveryMem(){
		$MemberModel = new MemberModel();
		$param = input('post.');
		$ids = explode(",",$param['ids']);
		return $MemberModel->recoveryMem($ids);
	}

	/**
	 * [客户/会员删除]
	 */
	public function delMem(){
		$MemberModel = new MemberModel();
		$param = input('post.');
		$ids = explode(",",$param['ids']);
		return $MemberModel->deleteMem($ids);
	}

	/**
	 * [订单回收站] 删除列表 恢复 删除
	 * 黄
	 */
	public function order ()
	{
		if(request()->isAjax()){
			$OrderModel = new OrderModel();
			$param = input('param.');
			$limit = $param['pageSize'];
			$offset = ($param['pageNumber'] - 1) * $limit;
			$where = [];
			if (!empty($param['stu_name'])) {
				$where['stu_name'] = ['like', '%' . $param['stu_name'] . '%'];
			}
			if (!empty($param['order_sn'])) {
				$where['order_sn'] = ['like', '%' . $param['order_sn'] . '%'];
			}
			$where['delete_status'] = ['neq',0];

			$orderArr = $OrderModel->getMyOrder($where, $offset, $limit);
			foreach ($orderArr as $key=>&$value){
				$u_id = $value['m_id'];
				$grade = $value['grade'];//层次
				$payment_method  = $value['payment_method'];//缴费方式
				$consultant = $value['consultant'];
				$mem_phone = Db::name('member')->where(['id'=>$u_id])->value('phone');//查询当前订单的客户信息
				$gradeN = Db::name('product_attr')->where(['id'=>$grade])->value('name');//获取当前层次
				$payment_methodN = Db::name('product_attr')->where(['id'=>$payment_method])->value('name');
				$consultantN = Db::name('user')->where('id',$consultant)->value('real_name');
				$value['phone'] = $mem_phone;
				$value['gradeN'] = $gradeN;
				$value['payment_methodN'] = $payment_methodN;
				$value['consultantN'] = $consultantN;
				$value['operate'] = showOperate($this->mkOrderBtn($value['id']));
			}
			$return['total'] = $OrderModel->getAllOrder($where);  // 总数据
			$return['rows'] = $orderArr;
			return json($return);
		}
		return $this->fetch();
	}
	/**
	 * [订单恢复]
	 */
	public function recoveryOrd(){
		$OrderModel = new OrderModel();
		$param = input('post.');
		$ids = explode(",",$param['ids']);
		return $OrderModel->recoveryOrd($ids);
	}

	/**
	 * [订单删除]
	 * 状态：0（未支付），1（已支付/退款），2（已逾期），3（已拒绝） 4.(同意)，5(已取消)
	 */
	public function delOrd(){
		$OrderModel = new OrderModel();
		$param = input('post.');
		$ids = explode(",",$param['ids']);
		return $OrderModel->deleteOrd($ids);
	}
	/**
	 * 客户/会员按钮
	 */
	private function mkMemberBtn($id){
		return [
			'<i class="fa fa-retweet"></i>' => [
				'auth' => 'trash/recoverymem',
				'href' => "javascript:recoveryMem(" . $id . ")",
				'btnStyle' => 'primary',
				'icon' => ''
			],
			'<i class="fa fa-trash-o">' => [
				'auth' => 'trash/delmem',
				'href' => "javascript:delMem(" . $id . ")",
				'btnStyle' => 'danger',
				'icon' => ''
			],

		];
	}
	/**
	 * 订单按钮
	 */
	private function mkOrderBtn($id){
		return [
			'<i class="fa fa-retweet"></i>' => [
				'auth' => 'trash/recoveryord',
				'href' => "javascript:recoveryOrd(" . $id . ")",
				'btnStyle' => 'primary',
				'icon' => ''
			],
			'<i class="fa fa-trash-o">' => [
				'auth' => 'trash/delord',
				'href' => "javascript:delOrd(" . $id . ")",
				'btnStyle' => 'danger',
				'icon' => ''
			],

		];
	}
}
